package com.demo.java.OD251_300.OD269;

import java.util.Scanner;
import java.util.regex.Pattern;

/**
 * @author bug菌
 * @Source 公众号：猿圈奇妙屋
 * @des： 【Excel 单元格数值统计】问题
 * @url： https://blog.csdn.net/weixin_43970743/article/details/146072421
 */
public class OdMain {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] params = scanner.nextLine().split(" ");
        int rows = Integer.parseInt(params[0]);
        int cols = Integer.parseInt(params[1]);

        String[][] matrix = new String[rows][cols];
        // 输入矩阵内容
        for (int i = 0; i < rows; i++) {
            matrix[i] = scanner.nextLine().split(" ");
        }

        // 遍历矩阵进行公式计算
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (matrix[i][j].charAt(0) == '=') {
                    // 如果是公式，则进行计算
                    if (matrix[i][j].contains("+")) {
                        String[] op = matrix[i][j].split("\\+");
                        String op1 = op[0];
                        String op2 = op[1];

                        int num1, num2;
                        // 解析第一个操作数
                        if (Pattern.matches("^-?\\d+(\\.\\d+)?$", op1.substring(1))) {
                            num1 = Integer.parseInt(op1.substring(1));
                        } else {
                            num1 = Integer.parseInt(matrix[Integer.parseInt(op1.substring(2)) - 1][op1.charAt(1) - 'A']);
                        }

                        // 解析第二个操作数
                        if (Pattern.matches("^-?\\d+(\\.\\d+)?$", op2)) {
                            num2 = Integer.parseInt(op2);
                        } else {
                            num2 = Integer.parseInt(matrix[Integer.parseInt(op2.substring(1)) - 1][op2.charAt(0) - 'A']);
                        }

                        matrix[i][j] = Integer.toString(num1 + num2);
                    } else if (matrix[i][j].contains("-")) {
                        String[] op = matrix[i][j].split("-");
                        String op1 = op[0];
                        String op2 = op[1];

                        int num1, num2;
                        // 解析第一个操作数
                        if (Pattern.matches("^-?\\d+(\\.\\d+)?$", op1.substring(1))) {
                            num1 = Integer.parseInt(op1.substring(1));
                        } else {
                            num1 = Integer.parseInt(matrix[Integer.parseInt(op1.substring(2)) - 1][op1.charAt(1) - 'A']);
                        }

                        // 解析第二个操作数
                        if (Pattern.matches("^-?\\d+(\\.\\d+)?$", op2)) {
                            num2 = Integer.parseInt(op2);
                        } else {
                            num2 = Integer.parseInt(matrix[Integer.parseInt(op2.substring(1)) - 1][op2.charAt(0) - 'A']);
                        }

                        matrix[i][j] = Integer.toString(num1 - num2);
                    } else {
                        matrix[i][j] = matrix[Integer.parseInt(matrix[i][j].substring(2)) - 1][matrix[i][j].charAt(1) - 'A'];
                    }
                }
            }
        }

        // 输入指定区域进行求和
        String output = scanner.nextLine();
        String[] outputParams = output.split(":");
        int res = 0;
        for (int i = Integer.parseInt(outputParams[0].substring(1)) - 1;
             i < Integer.parseInt(outputParams[1].substring(1)); i++) {
            for (int j = outputParams[0].charAt(0) - 'A';
                 j <= outputParams[1].charAt(0) - 'A'; j++) {
                res += Integer.parseInt(matrix[i][j]);
            }
        }
        System.out.println(res);
    }
}